'use strict';

// 引入nodejs库
let path = require('path');
let fs   = require('fs');

// 引入 gulp
let browserSync     = require('browser-sync').create();
let gulp            = require('gulp');
let minifycss       = require('gulp-clean-css');
let sass            = require('gulp-sass');
let del             = require('del');
let sourcemaps      = require('gulp-sourcemaps');
let watch           = require('nswatch');
let contentIncluder = require('gulp-content-includer');

let option = {base: 'src'};

// 开发模式
let isRelease = false;

/////////////////////// build //////////////////////////////////////
let distRoot = "dist/www";

gulp.task('build:style', function () {
    if (isRelease) {
        gulp.src(['src/css/**/*.scss'], option)
            .pipe(sass())
            .pipe(minifycss({rebase: false}))
            .pipe(gulp.dest(distRoot));

        // XXX 拷贝文件夹
        return gulp.src(['src/css/**/*.css'],
            option)
            .pipe(minifycss({rebase: false}))
            .pipe(gulp.dest(distRoot));
    }
    else {
        gulp.src(['src/css/**/*.scss'], option)
            .pipe(sourcemaps.init())
            .pipe(sass())
            // .pipe(postcss([autoprefixer]))
            .pipe(sourcemaps.write({includeContent: false, sourceRoot: '/src'}))
            .pipe(gulp.dest(distRoot));

        // XXX 拷贝文件夹
        return gulp.src(['src/css/**/*.css'],
            option)
            .pipe(sourcemaps.init())
            .pipe(sourcemaps.write({includeContent: false, sourceRoot: '/src'}))
            .pipe(gulp.dest(distRoot));
    }
});

/**
 * 静态资源文件
 */
gulp.task('build:html', function () {
    // XXX 拷贝文件夹
    return gulp.src([
            'src/**/*.html', "!src/**/*_module.html"
        ],
        option)
        .pipe(contentIncluder({
            includerReg: /<!\-\-include\s+"([^"]+)"\-\->/g
        }))
        .pipe(gulp.dest(distRoot));
});

/**
 * 静态资源文件
 */
gulp.task('build:assets', function () {
    // XXX 拷贝文件夹
    return gulp.src([
            'src/img/**/*',
            'src/libs/**/*',
        ],
        option)
        .pipe(gulp.dest(distRoot));
});

/////////////////////// run //////////////////////////////////////

let buildList = ['build:html', 'build:assets', 'build:style'];

gulp.task('build', function () {
    isRelease = false;
    gulp.run(buildList);
});

gulp.task('release', function () {
    isRelease = true;
    gulp.run(buildList);
});

// 监听测试页面
gulp.task('watch', function () {
    isRelease = false;
    gulp.watch([
        'src/img/**/*',
        'src/libs/**/*',
    ], ['build:assets']);

    gulp.watch([
        'src/**/*.html',
    ], ['build:html']);

    gulp.watch(['src/css/**/*.?(css|scss)'], ['build:style']);

    // watch("onTest.file", ['js-watch-tsc']);
    watch("onTest.file", ['js-watch-webpack']);

    browserSync.init({
        server: "./dist/www"
    });
});

gulp.task('default', function () {
    console.log("hello world");
});

gulp.task('clean', del.bind(null, [distRoot, './dist/template', './dist/hash', './dist/temp']));